package org.javahispano.javacup.tacticas.tacticas_aceptadas.masia;

import java.util.List;

import org.javahispano.javacup.modelo.Comando;
import org.javahispano.javacup.modelo.ComandoIrA;
import org.javahispano.javacup.modelo.Constantes;
import org.javahispano.javacup.modelo.Posicion;
import org.javahispano.javacup.modelo.SituacionPartido;

public class Attack {
	private SituacionPartido sp;
	private List<Comando> comandos;
	private final Data data;
	private final int goalkeeper = 0;
	private final Posicion positions1[]=new Posicion[]{
			new Posicion(0.2595419847328244,-50.41044776119403),
			new Posicion(-8.321678321678322,-20.192307692307693),
			new Posicion(5.706293706293707,-20.667420814479637),
			new Posicion(20.923076923076923,-20.90497737556561),
			new Posicion(-21.3986013986014,-19.95475113122172),
			new Posicion(0.0,-8.789592760180994),
			new Posicion(9.748251748251748,8.076923076923077),
			new Posicion(-10.6993006993007,7.601809954751132),
			new Posicion(21.16083916083916,26.606334841628957),
			new Posicion(0.4755244755244755,30.6447963800905),
			new Posicion(-18.06993006993007,24.705882352941178)
	    	};	
	
	 Posicion positions2[]=new Posicion[]{
		 	new Posicion(0.2595419847328244,-50.41044776119403),
	        new Posicion(-16.405594405594407,-23.755656108597286),
	        new Posicion(13.79020979020979,-24.705882352941178),
	        new Posicion(0.0,-24.468325791855204),
	        new Posicion(7.846153846153847,-5.226244343891403),
	        new Posicion(-7.37062937062937,-4.038461538461538),
	        new Posicion(-15.93006993006993,20.90497737556561),
	        new Posicion(0.4755244755244755,10.690045248868778),
	        new Posicion(16.643356643356643,23.28054298642534),
	        new Posicion(5.706293706293707,32.30769230769231),
	        new Posicion(-6.181818181818182,27.794117647058822)
		    };

	 Posicion positions3[]=new Posicion[]{
			new Posicion(0.2595419847328244,-50.41044776119403),
			new Posicion(-16.167832167832167,-18.054298642533936),
			new Posicion(14.503496503496503,-18.29185520361991),
			new Posicion(0.0,-18.529411764705884),
			new Posicion(-0.4755244755244755,-6.414027149321266),
			new Posicion(-9.034965034965035,8.789592760180994),
			new Posicion(-0.951048951048951,19.479638009049776),
			new Posicion(8.55944055944056,9.027149321266968),
			new Posicion(13.076923076923078,28.744343891402718),
			new Posicion(-2.8531468531468533,35.63348416289593),
			new Posicion(-14.97902097902098,26.368778280542987)
		    };
	
	public Attack(List<Comando> comandos, Data data) {		
		this.comandos = comandos;
		this.data = data;
	}
	
	public void execute(SituacionPartido sp, Mentality mentality){
		this.sp = sp;
		Posicion[] newPositions = calculatePositions(mentality == Mentality.Normal? positions1: 
													mentality == Mentality.Offensive? positions2: positions3);
		for (int i = 0; i < newPositions.length; i++) {
			if(i != goalkeeper) {
				comandos.add(new ComandoIrA(i, newPositions[i]));
			}
		}		
		getBall();
	}

	private void getBall() {
		if(data.getIterToBall() >= 0)
			comandos.add(new ComandoIrA(data.getPlayerClosetBall(), data.getPosBall(data.getIterToBall())));		
	}

	private Posicion[] calculatePositions(Posicion[] positions){
		double height = Math.max(Constantes.LARGO_CAMPO_JUEGO/3, 2*Math.min(Math.abs(Constantes.centroArcoInf.getY()-sp.balon().getY()), Math.abs(Constantes.centroArcoSup.getY()-sp.balon().getY())));
		double width = Math.max(Constantes.ANCHO_CAMPO_JUEGO*2/3, 2*Math.min(Math.abs(Constantes.cornerInfIzq.getX()-sp.balon().getX()), Math.abs(Constantes.cornerInfDer.getX()-sp.balon().getX())));		
		Posicion center = new Posicion(sp.balon().getX(), sp.balon().getY());
		if(center.getX() + width/2 > Constantes.cornerInfDer.getX()){
			center = center.moverPosicion(-(center.getX() + width/2 - Constantes.cornerInfDer.getX()), 0);
		}
		if(center.getX() - width/2 < Constantes.cornerInfIzq.getX()){
			center = center.moverPosicion(Constantes.cornerInfIzq.getX() - (center.getX()-width/2), 0);
		}
		if(center.getY() + height/2 > Constantes.centroArcoSup.getY()){
			center = center.moverPosicion(0, -(center.getY() + height/2 - Constantes.centroArcoSup.getY()));
		}
		if(center.getY() - height/2 < Constantes.centroArcoInf.getY()){
			center = center.moverPosicion(0, Constantes.centroArcoInf.getY() - (center.getY() - height/2));
		}
		Posicion[] newPositions = new Posicion[11];
		for (int i = 0; i < newPositions.length; i++) {
			newPositions[i] = new Posicion(positions[i].getX()*width/Constantes.ANCHO_CAMPO_JUEGO + center.getX(), positions[i].getY()*height/Constantes.LARGO_CAMPO_JUEGO + center.getY());
		}
		return newPositions;
	}
}
